package com.redsoil.house.ui.matter.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.redsoil.house.R;
import com.redsoil.house.base.RootActivity;
import com.redsoil.house.model.bean.OneMessageVO;
import com.redsoil.house.model.bean.RoomFileVO;
import com.redsoil.house.ui.matter.contract.MatterContract;
import com.redsoil.house.ui.matter.contract.MatterInfoContract;
import com.redsoil.house.ui.matter.presenter.MatterInfoPresenter;
import com.redsoil.house.ui.matter.presenter.MatterPresenter;
import com.redsoil.house.widget.PropertyFileItem;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 待确定事项详情
 */
public class MatterInfoActivity extends RootActivity<MatterInfoPresenter> implements MatterInfoContract.View,XBaseTopBarImp{

    @BindView(R.id.matter_date_tv)
    TextView matter_date_tv;

    @BindView(R.id.matter_title_tv)
    TextView matter_title_tv;

    @BindView(R.id.matter_content_tv)
    TextView matter_content_tv;

    @BindView(R.id.business_related)
    PropertyFileItem business_related;

    @BindView(R.id.assets_property)
    PropertyFileItem assets_property;

    @BindView(R.id.tenancy_management)
    PropertyFileItem tenancy_management;

    @BindView(R.id.insurance_related)
    PropertyFileItem insurance_related;

    @BindView(R.id.tax_related)
    PropertyFileItem tax_related;

    @BindView(R.id.other)
    PropertyFileItem other;

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @BindView(R.id.number_edit)
    EditText number_edit;

    @BindView(R.id.sure_button)
    Button sure_button;

    @BindView(R.id.equal_button)
    Button equal_button;

    @BindView(R.id.matter_state_tv)
    TextView matter_state_tv;

    @BindView(R.id.matter_room_tv)
    TextView matter_room_tv;

    private OneMessageVO oneMessageVO;

    @BindView(R.id.button_layout)
    AutoLinearLayout button_layout;

    @Override
    protected int getLayout() {
        return R.layout.activity_matter_info;
    }

    @Override
    protected void initEventAndData() {

        super.initEventAndData();

        stateLoading();

        main_topbar.setxBaseTopBarImp(this);

        mPresenter.getOneMessage(getIntent().getStringExtra("id"));
    }

    @Override
    public void getOneMessageSuccess(OneMessageVO oneMessageVO) {

        this.oneMessageVO = oneMessageVO;

        matter_date_tv.setText(oneMessageVO.getSendTime());
        matter_title_tv.setText(oneMessageVO.getTitle() );
        if(StringUtils.isSpace(oneMessageVO.getRoomNum())){
            matter_room_tv.setVisibility(View.GONE);
        }else {
            matter_room_tv.setText("# "+oneMessageVO.getRoomNum());
        }

        matter_content_tv.setText(oneMessageVO.getContent());
        matter_state_tv.setText(oneMessageVO.getTypeName());

        //LEASE 租赁确认
        //WORK 施工确认
        //RENT 租金确认

        //这里逻辑 真ok
        if(oneMessageVO.getTypeCode().equals("WORK") || oneMessageVO.getTypeCode().equals("RENT")){
            number_edit.setVisibility(View.GONE);
            if(oneMessageVO.getIsSure().equals("1")){
                sure_button.setText("已驳回");
                equal_button.setText("同意");
                equal_button.setVisibility(View.INVISIBLE);
                sure_button.setEnabled(false);
            }else {
                sure_button.setText("驳回");
                equal_button.setText("同意");
            }

        }else{

            if(oneMessageVO.getIsSure().equals("1")){
                equal_button.setVisibility(View.GONE);
                number_edit.setText(oneMessageVO.getNowPrice()+"");
                number_edit.setEnabled(false);
                number_edit.setFocusable(false);
                number_edit.setKeyListener(null);

                sure_button.setText("已确认");
                sure_button.setEnabled(false);
            }

//            if(oneMessageVO.getTypeName().equals("租赁确认")){
//                if(oneMessageVO.getIsSure().equals("1")){
//
//                    equal_button.setVisibility(View.GONE);
//
//                    number_edit.setText(oneMessageVO.getNowPrice()+"");
//                    number_edit.setEnabled(false);
//                    number_edit.setFocusable(false);
//                    number_edit.setKeyListener(null);
//
//                    sure_button.setText("已经确认");
//                    sure_button.setEnabled(false);
//                }
//            }else {
//                equal_button.setVisibility(View.GONE);
//            }
        }

        // 魏定强 新增 如果类型为 OTHER , 则全部隐藏
        if(oneMessageVO.getTypeCode().equals("OTHER")){
            equal_button.setVisibility(View.INVISIBLE);
            number_edit.setVisibility(View.INVISIBLE);
            sure_button.setVisibility(View.INVISIBLE);
        }


//        else {
//            matter_state_tv.setVisibility(View.GONE);
//        }

        for (int i= 0;i<oneMessageVO.getFiles().size();i++){
            OneMessageVO.FilesBeanX filesBeanx = oneMessageVO.getFiles().get(i);
            switch (filesBeanx.getTypeCode()){
                case "MMXG":
                    setPropertyFileItemData(business_related,filesBeanx,R.drawable.icon_business_related,getResources().getString(R.string.business_related),getResources().getDrawable(R.drawable.shape_top_edge_white));
                    break;
                case "ZWGX":
                    setPropertyFileItemData(assets_property,filesBeanx,R.drawable.icon_assets_property,getResources().getString(R.string.assets_property),getResources().getDrawable(R.drawable.shape_top_edge_white));
                    break;
                case "ZYGL":
                    setPropertyFileItemData(tenancy_management,filesBeanx,R.drawable.icon_tenancy_management,getResources().getString(R.string.tenancy_management),getResources().getDrawable(R.drawable.shape_top_edge_white));
                    break;
                case "BXXG":
                    setPropertyFileItemData(insurance_related,filesBeanx,R.drawable.icon_insurance_related,getResources().getString(R.string.insurance_related),getResources().getDrawable(R.drawable.shape_top_edge_white));
                    break;
                case "SWXG":
                    setPropertyFileItemData(tax_related,filesBeanx,R.drawable.icon_tax_related,getResources().getString(R.string.tax_related),getResources().getDrawable(R.drawable.shape_top_edge_white));
                    break;
                case "OTHER":
                    setPropertyFileItemData(other,filesBeanx,R.drawable.icon_other,getResources().getString(R.string.other),getResources().getDrawable(R.drawable.shape_top_bottom_edge_white));
                    break;
                default:
                    break;
            }
        }
        stateMain();
    }

    private void setPropertyFileItemData(PropertyFileItem propertyFileItem, final OneMessageVO.FilesBeanX filesBeanx, Integer icon,String title,Drawable drawable){
        propertyFileItem.setData(icon,title,filesBeanx.getFileCount()+"",drawable);
        if(filesBeanx.getFileCount() > 0){
            propertyFileItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ArrayList<RoomFileVO.FilesBeanX.FilesBean> arrayList = new ArrayList<RoomFileVO.FilesBeanX.FilesBean>();
                    for (int i=0;i<filesBeanx.getFiles().size();i++){
                        OneMessageVO.FilesBeanX.FilesBean beanx = (OneMessageVO.FilesBeanX.FilesBean)filesBeanx.getFiles().get(i);
                        RoomFileVO.FilesBeanX.FilesBean bean = new RoomFileVO.FilesBeanX.FilesBean();

                        bean.setFileName(beanx.getFileName());
                        bean.setFilePath(beanx.getFilePath());
                        arrayList.add(bean);
                    }
                    startActivity(MatterFileListActivity.newInstance(getBaseContext(),arrayList));
                }
            });
        }
    }

    @OnClick(R.id.sure_button)
    void affirm(){
        String number = number_edit.getText().toString();
//        if(StringUtils.isSpace(number)){
//            ToastUtils.showShortToast("请填入数字");
//            return;
//        }

        mPresenter.affirm(oneMessageVO.getId(),number,"0");
    }

    @OnClick(R.id.equal_button)
    void equalButton(){
        mPresenter.affirm(oneMessageVO.getId(),oneMessageVO.getNowPrice(),"1");
    }

    @Override
    public void affirmSuccess() {
        ToastUtils.showShortToast("确认成功");
        button_layout.setVisibility(View.GONE);
    }

    @Override
    public void responeError(String errorMsg) {
        ToastUtils.showShortToast("确认失败");
    }

    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {}

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context,String id) {
        Intent intent = new Intent(context, MatterInfoActivity.class);
        intent.putExtra("id",id);
        return intent;
    }

}
